package com.aglow.bluetoothspeaker.main.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.aglow.bluetoothspeaker.R;
import com.aglow.bluetoothspeaker.main.ui.activity.SetDataActivity;
import com.aglow.bluetoothspeaker.model.bean.AlarmEditorInfo;
import com.aglow.bluetoothspeaker.model.bean.AlarmSetDate;
import com.aglow.bluetoothspeaker.presenter.lisnter.OnItemDeleteListener;
import com.aglow.bluetoothspeaker.presenter.lisnter.OnSwitchCheckListener;
import com.alex.alexswitch.ISwitch;
import com.facebook.stetho.common.LogUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: chufeng
 * Email: 392044178@qq.com
 * Date: 2018/9/12 11:06
 */
public class AlarmAdapter extends RecyclerView.Adapter<AlarmAdapter.ViewHolder> {


    private final Context mContext;
    private final List<AlarmSetDate> list;
    private final LayoutInflater inflater;


    private OnSwitchCheckListener mSwtichCheckListener;
    private OnItemDeleteListener onItemDeleteListener;
    private String[] dates = new String[]{"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
    private String setHour;
    private String setMinute;
    private String everdayStr = "Sun" + " "+"Mon" + " "+"Tue" + " "+"Wed" + " "+"Thu" + " "+"Fri" + " "+"Sat" + " ";
    private String weekendStr = "Sun" + " "+"Sat" + " ";
    private String weekdaysStr =  "Mon" + " "+"Tue" + " "+"Wed" + " "+"Thu" + " "+"Fri" + " ";

    public AlarmAdapter(Context context, List<AlarmSetDate> list) {
        this.mContext = context;
        this.list = list;
        inflater = LayoutInflater.from(mContext);
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_alarm, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder( ViewHolder holder,int position) {

        holder.swtich.setOnISwitchOnClickListener(new ISwitch.ISwitchOnClickListeners() {
            @Override
            public void open() {
                if (mSwtichCheckListener != null) {
                    mSwtichCheckListener.open(position);
                }
            }

            @Override
            public void close() {
                if (mSwtichCheckListener != null) {
                    mSwtichCheckListener.close(position);
                }
            }
        });
        holder.tvTouch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int adapterPosition = holder.getAdapterPosition();
//                Toast.makeText(mContext, "onClick:" + adapterPosition, Toast.LENGTH_SHORT).show();
                Intent intent  = new Intent(mContext, SetDataActivity.class);
                mContext.startActivity(intent);
                AlarmSetDate alarmSetDate = list.get(adapterPosition);
                AlarmEditorInfo alarmEditorInfo = new AlarmEditorInfo();
                alarmEditorInfo.setDate(alarmSetDate.getDate());
                alarmEditorInfo.setHour(alarmSetDate.getHour());
                alarmEditorInfo.setLampState(alarmSetDate.isLampState());
                alarmEditorInfo.setMinute(alarmSetDate.getMinute());
                alarmEditorInfo.setSoundState(alarmSetDate.isSoundState());
                alarmEditorInfo.setAlarmIndex(alarmSetDate.isIndex());
                alarmEditorInfo.setPosition(adapterPosition);
                alarmEditorInfo.setPlayState(alarmSetDate.isOpenState());
                EventBus.getDefault().postSticky(alarmEditorInfo);

            }
        });
        holder.btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemDeleteListener.delete(holder.getAdapterPosition());
            }
        });
        AlarmSetDate alarmSetDate = list.get(position);
        boolean[] date = alarmSetDate.getDate();
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < date.length; i++) {
            if (date[i] == true) {
                stringBuffer.append(dates[i] + " ");

            }
        }
        int hour = alarmSetDate.getHour();
        Log.i("adapter", "hour = " + hour);
        int minute = alarmSetDate.getMinute();
        Log.i("adapter", "minute = " + minute);
        boolean lampState = alarmSetDate.isLampState();
        boolean soundState = alarmSetDate.isSoundState();
        boolean openState = alarmSetDate.isOpenState();
        Log.i("adapter", "openState = " + openState);
        if (openState){
            holder.swtich.setIsOpen(true);
        }else {
            holder.swtich.setIsOpen(false);
        }
        Log.i("AlarmAdapter","lampState = "+lampState);
        Log.i("AlarmAdapter","soundState = "+soundState);
        if (lampState) {
            holder.imageView3.setVisibility(View.VISIBLE);
            Log.i("AlarmAdapter","显示lamp咯!!!" );
        } else {
            holder.imageView3.setVisibility(View.INVISIBLE);
            Log.i("AlarmAdapter","隐藏lamp咯!!!" );
        }

        if (soundState) {
            holder.imageView4.setVisibility(View.VISIBLE);
            Log.i("AlarmAdapter","显示sound咯!!!" );
        } else {
            holder.imageView4.setVisibility(View.INVISIBLE);
            Log.i("AlarmAdapter","隐藏sound咯!!!" );
        }
        setHour = String.valueOf(hour);
        setMinute = String.valueOf(minute);
        if (stringBuffer.length()==0){
            holder.texDate.setText("Once");
        }else {
            if (stringBuffer.toString().equals(everdayStr)){
                holder.texDate.setText("Every day");
            }else if (stringBuffer.toString().equals(weekdaysStr)){
                holder.texDate.setText("Weekdays");
            }else if (stringBuffer.toString().equals(weekendStr)){
                holder.texDate.setText("Weekend");
            }else {
                holder.texDate.setText(stringBuffer);
            }
        }
        if (setHour.length() == 1) {
            setHour = "0" + setHour;
        }
        if (setMinute.length() == 1) {
            setMinute = "0" + setMinute;
        }
        holder.textView5.setText(setHour + ":" + setMinute);
        holder.swtich.setTag(position);


    }

    @Override
    public int getItemCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    public void setSwitchCheckListener(OnSwitchCheckListener mSwtichCheckListener) {
        this.mSwtichCheckListener = mSwtichCheckListener;
    }

    public void setDeleteItemListener(OnItemDeleteListener onItemDeleteListener) {
        this.onItemDeleteListener = onItemDeleteListener;
    }


    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.id_apple_is)
        ISwitch swtich;
        @BindView(R.id.textView5)
        TextView textView5;
        @BindView(R.id.imageView4)
        ImageView imageView4;
        @BindView(R.id.imageView3)
        ImageView imageView3;
        @BindView(R.id.btnDelete)
        Button btnDelete;
        @BindView(R.id.tex_date)
        TextView texDate;
        @BindView(R.id.tv_touch)
        TextView tvTouch;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


}
